/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "TestRecord.h"
#include "types.h"
#include "Logger.h"
#include "Options.h"

TestRecord::TestRecord() :
	mPassCount(0),
	mFailCount(0)
{
	mTestDate = OPT.getCurrendDate();
	mTestDateString = mTestDate.toString(DATE_FORMAT);
	mTestRecordString = mTestDateString + QString(":");
}

TestRecord::TestRecord(QDate date, QString resultString) :
	mTestDate(date),
	mTestResultString(resultString),
	mPassCount(0),
	mFailCount(0)
{
	mTestDateString = mTestDate.toString(DATE_FORMAT);
	mTestRecordString = mTestDateString + QString(":") + mTestResultString;
	decodeTestRecord();
}

TestRecord::~TestRecord() {
	// TODO Auto-generated destructor stub
}

void TestRecord::pass()
{
	mTestResultVector.push_back(true);
	mPassCount++;

	mTestResultString = encodeTestRecord();
	mTestRecordString = mTestDateString + QString(":") + mTestResultString;
}

void TestRecord::fail()
{
	mTestResultVector.push_back(false);
	mFailCount++;

	mTestResultString = encodeTestRecord();
	mTestRecordString = mTestDateString + QString(":") + mTestResultString;
}

bool TestRecord::passFirst()
{
	if (mTestResultVector.empty())
		return false;

	return mTestResultVector[0];
}

QString TestRecord::encodeTestRecord()
{
	QString newTestRecordString;
	//encode
	bool lastItem;
	int seriCount;
	if (mTestResultVector[0] == true)
	{
		lastItem = true;
		seriCount = 1;
	}
	else
	{
		lastItem = false;
		seriCount = 1;
	}

	for (int j=1; j<mTestResultVector.size(); j++)
	{
		if (mTestResultVector[j] == lastItem)
			seriCount++;
		else
		{
			if (lastItem == true)
				newTestRecordString.append(QString("Y%1").arg(seriCount));
			else
				newTestRecordString.append(QString("N%1").arg(seriCount));

			lastItem = mTestResultVector[j];
			seriCount = 1;
		}
	}

	if (lastItem == true)
		newTestRecordString.append(QString("Y%1").arg(seriCount));
	else
		newTestRecordString.append(QString("N%1").arg(seriCount));

	return newTestRecordString;
}


void TestRecord::decodeTestRecord()
{
	mTestResultVector.clear();
	mPassCount = 0;
	mFailCount = 0;

	bool lastItem = true;
	int seriCount = 0;
	QString count;
	count.clear();
	for (int j=0; j<mTestResultString.size(); j++)
	{
		if (mTestResultString[j] == 'Y' || mTestResultString[j] == 'N')
		{
			if (!count.isEmpty())
				for (int k=0; k<count.toInt(); k++)
				{
					mTestResultVector.push_back(lastItem);
					if (lastItem == true)
						mPassCount++;
					else
						mFailCount++;
				}

			if (mTestResultString[j] == 'Y')
			{
				lastItem = true;
				count.clear();
			}
			else
			{
				lastItem = false;
				count.clear();
			}
			seriCount = 1;
		}
		else
			count.append(mTestResultString[j]);
	}
	if (!count.isEmpty())
	{
		for (int k=0; k<count.toInt(); k++)
		{
			mTestResultVector.push_back(lastItem);
			if (lastItem == true)
				mPassCount++;
			else
				mFailCount++;
		}
	}
}

int TestRecord::getInterval()
{
	QDate today = OPT.getCurrendDate();
	int interval = mTestDate.daysTo(today);
	return interval;
}
